package cn.jcet.module.bus.controller;


import cn.jcet.common.utils.Constast;
import cn.jcet.common.utils.DataGridView;
import cn.jcet.common.utils.ResultObj;
import cn.jcet.module.bus.domain.Provider;
import cn.jcet.module.bus.service.IProviderService;
import cn.jcet.module.bus.vo.ProviderVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private IProviderService providerService;

    /**
     * 加载全部
     * @param providerVO
     * @return
     */
    @GetMapping("loadAllProvider")
    public DataGridView loadAllProvider(ProviderVo providerVO){
        IPage<Provider> page = new Page<>(providerVO.getPage(),providerVO.getLimit());

        QueryWrapper<Provider> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(providerVO.getProvidername()),"providername",providerVO.getProvidername());
        queryWrapper.like(StringUtils.isNotBlank(providerVO.getPhone()),"phone",providerVO.getPhone());
        queryWrapper.like(StringUtils.isNotBlank(providerVO.getConnectionperson()), "connectionperson",providerVO.getConnectionperson());

        this.providerService.page(page,queryWrapper);

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加
     * @param providerVO
     * @return
     */
    @PostMapping("addProvider")
    public ResultObj addProvider(ProviderVo providerVO){
        try {
            this.providerService.save(providerVO);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     * @param providerVO
     * @return
     */
    @PostMapping("updateProvider")
    public ResultObj updateProvider(ProviderVo providerVO){
        try {
            this.providerService.updateById(providerVO);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("deleteProvider")
    public ResultObj deleteProvider(Integer id){
        try {
            this.providerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @PostMapping("batchDeleteProvider")
    public ResultObj batchDeleteProvider(ProviderVo providerVo){

        try {
            Collection<Serializable> listIds = new ArrayList<>();

            for (Serializable ids : providerVo.getIds()){
                listIds.add(ids);
            }
            this.providerService.removeByIds(listIds);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @GetMapping("loadAllProviderForSelect")
    public DataGridView loadAllProviderForSelect(){
        QueryWrapper<Provider> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("available",Constast.AVAILABLE_TRUE);
        List<Provider> list = this.providerService.list(queryWrapper);
        return new DataGridView(list);
    }
}
